import 'package:sainbainu/common/apis/group_api.dart';
import 'package:sainbainu/common/models/group_manage_info.dart';
import 'package:sainbainu/common/models/request/pager_request.dart';
import 'package:sainbainu/common/models/response/content_response.dart';
import 'package:sainbainu/common/models/response/group_detail_response.dart';
import 'package:sainbainu/common/models/response/group_list_response.dart';
import 'package:sainbainu/common/models/response/group_member_response.dart';

class GroupRepository {
  final GroupApi _groupApi;

  GroupRepository(this._groupApi);

  /// 创建小组
  Future<Map<String, dynamic>> create(
      GroupManageInfoModel groupManageInfoModel) async {
    var res = await _groupApi.create(groupManageInfoModel.toJson());
    return res;
  }

  /// 编辑小组
  Future<Map<String, dynamic>> update(
      GroupManageInfoModel groupManageInfoModel) async {
    var res = await _groupApi.update(groupManageInfoModel.toJson());
    return res;
  }

  /// 小组编辑详情
  Future<GroupManageInfoModel> info(int groupId) async {
    var res = await _groupApi.info({'groupId': groupId});
    return GroupManageInfoModel.fromJson(res['data']);
  }

  /// 我的小组列表
  Future<GroupListResponse> myList(
    PagerRequest pagerRequest, {
    int? creatorFlag,
  }) async {
    var params = pagerRequest.toJson();
    if (creatorFlag != null) {
      params['creatorFlag'] = creatorFlag;
    }
    var res = await _groupApi.myList(params);
    return GroupListResponse.fromJson(res);
  }

  /// 推荐小组列表
  Future<GroupListResponse> recommentList(PagerRequest pagerRequest) async {
    var res = await _groupApi.recommentList(pagerRequest.toJson());
    return GroupListResponse.fromJson(res);
  }

  /// 详细信息
  Future<GroupDetailResponse> detail(int groupId) async {
    var res = await _groupApi.detail({'groupId': groupId});
    return GroupDetailResponse.fromJson(res['data']);
  }

  /// 加入小组
  Future<Map<String, dynamic>> join(int groupId) async {
    var res = await _groupApi.join({'groupId': groupId});
    return res;
  }

  /// 退出小组
  Future<Map<String, dynamic>> unjoin(int groupId) async {
    var res = await _groupApi.unjoin({'groupId': groupId});
    return res;
  }

  /// 小组帖子列表
  Future<ContentListResponse> groupContentList(
    PagerRequest pagerRequest,
    int groupId,
  ) async {
    Map<String, dynamic> params = pagerRequest.toJson();
    params['groupId'] = groupId;

    var res = await _groupApi.groupContentList(params);
    return ContentListResponse.fromJson(res);
  }

  /// 成员列表
  Future<GroupMemberListResponse> memberList(
    PagerRequest pagerRequest,
    int groupId,
  ) async {
    Map<String, dynamic> params = pagerRequest.toJson();
    params['groupId'] = groupId;

    var res = await _groupApi.memberList(params);
    return GroupMemberListResponse.fromJson(res);
  }

  /// 移除成员
  Future<Map<String, dynamic>> dropMember(int memberId) async {
    var res = await _groupApi.dropMember({'memberId': memberId});
    return res;
  }
}
